package coupon

import (
	sqlc "bp/db/sqlc"
	"bp/internal/render"
	// sharedConv "bp/internal/rpcserver/shared/conv"
	sharedVld "bp/internal/rpcserver/shared/vld"
	"bp/internal/service/entity/coupon"
	memberCoupon "bp/internal/service/entity/member_coupon"
	"bp/internal/util"
	couponGrpc "bp/rpc/gen/mwebapi/mcoupon"
	"context"
)

// VldGetCouponReq 验证获取优惠券请求
func (s *CouponMWebApiImp) VldGetCouponReq(ctx context.Context, q *sqlc.Queries, req *couponGrpc.GetCouponReq) (
	couponPtr *sqlc.Coupon,
	available bool,
	err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	token := util.GetAuthorizationToken(ctx)
	if req.CouponId < 0 {
		err = render.InvalidArgumentRange("id")
		return
	}
	if !sharedVld.IsIssuer(req.Issuer) {
		err = render.InvalidArgumentValue("issuer")
		return
	}
	if !sharedVld.IsCouponDistributeScene(req.DistributeScene) {
		err = render.InvalidArgumentValue("distribute_scene")
		return
	}
	couponID := req.CouponId
	couponPtr, err = s.entityHub.CouponService.GetCouponPtr(ctx, q, couponID)
	if err != nil {
		return
	}
	// 检测优惠券状态
	if couponPtr.CouponStatus != string(sqlc.EmCouponStatusPublished) {
		available = false
		return
	}
	memberID := token.MemberID
	// slog.Infof("memberID:%d", memberID)

	// 检测领取人对该券是否达到最大领取数
	isReachMaximumGotFrequency, err := s.entityHub.MemberCouponService.CheckHasReachMaxiumGotFrequencyToMember(ctx, q, &memberCoupon.CheckHasReachMaxiumGotFrequencyToMemberParam{
		CouponPtr: couponPtr,
		MemberID:  memberID,
	})
	if err != nil {
		return
	}
	if isReachMaximumGotFrequency {
		available = false
		return
	}

	// 检测是否有可用的coupon sn
	hasAnyCouponSnCanGot, err := s.entityHub.CouponService.CheckHasAnyCouponSnCanGot(ctx, q, &coupon.CheckHasAnyCouponSnCanGotParam{
		CouponPtr:       couponPtr,
		Issuer:          req.Issuer,
		DistributeScene: req.DistributeScene,
	})
	if err != nil {
		return
	}
	if !hasAnyCouponSnCanGot {
		available = false
		return
	}

	available = true

	slog.Infof("coupon_imp/query_vld.VldGetCouponReq ok")
	return
}

// VldGetMemberCouponReq 验证获取会员优惠券请求
func (s *CouponMWebApiImp) VldGetMemberCouponReq(ctx context.Context, q *sqlc.Queries, req *couponGrpc.GetMemberCouponReq) (memberCouponID int64, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	if req.Id < 0 {
		err = render.InvalidArgumentRange("id")
		return
	}
	memberCouponID = req.Id
	slog.Infof("coupon_imp/query_vld.VldGetMemberCouponReq ok")
	return
}

// VldListCouponWithDistributionStatsReq 验证列出优惠券及其投放统计请求
func (s *CouponMWebApiImp) VldListCouponWithDistributionStatsReq(ctx context.Context, q *sqlc.Queries, req *couponGrpc.ListCouponWithDistributionStatsReq) (ptr *coupon.ListCouponWithDistributionStatPtrsParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	if req.OwnerId < 0 {
		err = render.InvalidArgumentRange("owner_id")
		return
	}
	if !sharedVld.IsIssuer(req.Issuer) {
		err = render.InvalidArgumentValue("issuer")
		return
	}
	if !sharedVld.IsCouponDistributeScene(req.DistributeScene) {
		err = render.InvalidArgumentValue("distribute_scene")
		return
	}
	ptr = &coupon.ListCouponWithDistributionStatPtrsParam{
		OwnerID:         req.OwnerId,
		Issuer:          req.Issuer,
		DistributeScene: req.DistributeScene,
	}
	slog.Infof("coupon_imp/query_vld.VldListCouponWithDistributionStatsReq ok")
	return
}

// VldListMemberCouponsReq 验证列出会员优惠券请求
func (s *CouponMWebApiImp) VldListMemberCouponsReq(ctx context.Context, q *sqlc.Queries, req *couponGrpc.ListMemberCouponsReq) (ptr *memberCoupon.ListMemberCouponPtrsParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	var memberCouponStatuses []string
	if !util.IsEmpty(req.MemberCouponStatus) {
		if !sharedVld.IsMemberCouponStatus(req.MemberCouponStatus) {
			err = render.InvalidArgumentValue("member_coupon_status")
			return
		}
		memberCouponStatuses = append(memberCouponStatuses, req.MemberCouponStatus)
	}

	ptr = &memberCoupon.ListMemberCouponPtrsParam{
		MemberCouponStatuses: memberCouponStatuses,
		Keyword:              req.Keyword,
	}
	slog.Infof("coupon_imp/query_vld.VldGetCouponReq ok")
	return
}

// VldListAvailableMemberCouponsReq 验证列出可用的会员优惠券请求
func (s *CouponMWebApiImp) VldListAvailableMemberCouponsReq(ctx context.Context, q *sqlc.Queries, req *couponGrpc.ListAvailableMemberCouponsReq) (ptr *memberCoupon.ListAvailableMemberCouponsOfUseSceneParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	token := util.GetAuthorizationToken(ctx)
	if !sharedVld.IsOfferUseScene(req.UseScene) {
		err = render.InvalidArgumentValue("use_scene")
		return
	}
	ptr = &memberCoupon.ListAvailableMemberCouponsOfUseSceneParam{
		BrandID:  token.BrandID,
		MemberID: token.MemberID,
		UseScene: req.UseScene,
	}
	slog.Infof("offer_imp/query_vld.VldListAvailableMemberCouponsReq ok")
	return
}
